package com.soft.servlet;

import com.soft.entity.Order;
import com.soft.entity.OrderReturnRecord;
import com.soft.entity.UserInfo;
import com.soft.service.Domain;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author kongz
 * @version 1.0 2021/6/9 10:10
 * @description 用户申请退货
 */
@WebServlet(name = "TuiHuoServlet", urlPatterns = "/TuiHuo")
public class TuiHuoServlet extends HttpServlet {
    private Domain domain;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();
        UserInfo ui = null;
        if (req.getSession().getAttribute("user") != null)
            ui = (UserInfo) req.getSession().getAttribute("user");
        String orderId = req.getParameter("orderId");
        if (domain.getOrderById(Integer.parseInt(orderId)).getStatus() == Order.STATUS.STATUS_CLOSED)
            req.getSession().setAttribute("flag", 50);
        else if (domain.getOrderReturnRecordByOrderId(Integer.parseInt(orderId)) == null) {
            req.setAttribute("orderId", orderId);
            req.setAttribute("address", ui.getAddress());
        } else
            req.getSession().setAttribute("flag", 46);
        req.getRequestDispatcher("tuihuo.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();

        String orderId = req.getParameter("orderId");
        String address = req.getParameter("address");
        String reason = req.getParameter("reason");

        OrderReturnRecord ord = new OrderReturnRecord();
        ord.setOrderId(orderId);
        ord.setAddress(address);
        ord.setReason(reason);

        if (domain.insertOrderReturnRecord(ord))
            req.getSession().setAttribute("flag", 44);
        else
            req.getSession().setAttribute("flag", 45);
        req.getRequestDispatcher("tuihuo.jsp").forward(req, resp);
    }
}
